package ca.ets.osgi.reservationservice;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;

public class ListRoomsInfo {

	//used for making the day
	public static final String[] dayTab = { "01", "02", "03", "04", "05", "06",
			"07", "08", "09", "10","11", "12", "13", "14", "15", "16",
			"17", "18", "19", "20","21", "22", "23", "24", "25", "26",
			"27", "28", "29", "30","31" };
	
	//used to make the month
	public static final String []monthTab = {"01", "02", "03", "04", "05", "06",
		"07", "08", "09", "10","11", "12"};
	
	//allows these years
	public static final String []yearTab = {"13"};
	
	//used for the creation of the start of the hour
	public static final String []hourStartTab = {"08", "09", "10","11", "12", "13", "14", "15", "16",
		"17", "18", "19", "20","21"};
	
	//used for the creation of the end of the hour
	public static final String []hourEndTab = {"09", "10","11", "12", "13", "14", "15", "16",
		"17", "18", "19", "20","21", "22"};
	
	//used for making the room available or not
	public static final boolean[] availabilityTab = {true,false};
	
	private Random generator = new Random();
	
	private Hashtable<String,RoomInfo> hTable = new Hashtable<String,RoomInfo>();
	
	//
	private ArrayList<RoomInfo> list = new ArrayList<RoomInfo>();
	
	//making a singleton class
	private static ListRoomsInfo instance;
	
	//returning the instance
	public static ListRoomsInfo getInstance(){
		if(instance ==null)
			instance  = new ListRoomsInfo();
		return instance;
	}

	/**
	 * @return the hTable
	 */
	public Hashtable<String, RoomInfo> gethTable() {
		return hTable;
	}

	/**
	 * @param hTable the hTable to set
	 */
	public void sethTable(Hashtable<String, RoomInfo> hTable) {
		this.hTable = hTable;
	}
	
	
	
	/**
	 * @return the list
	 */
	public ArrayList<RoomInfo> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(ArrayList<RoomInfo> list) {
		this.list = list;
	}
	
	/**
	 * 
	 * @param room
	 */
	public void addRoomInfoToList(RoomInfo room){
		this.list.add(room);
	}

	/**
	 * return whether a room is indeed in the table
	 * @param roomID
	 * @return
	 */
	public boolean isRoomPresent(String roomID){
		boolean response = false;
		RoomInfo room = this.hTable.get(roomID);
		if(room !=null)
			response = true;
		return response;
	}
	
	/**
	 * Add a room to the list
	 * @param roomID
	 * @param room
	 */
	public void addRoomInfoToTable(String roomID, RoomInfo room){
		if(!this.isRoomPresent(roomID)){
			this.hTable.put(roomID,room );
			System.out.println("Room " + room.getRoomID()+ " was added to the table");
		}
		
	}
	
	/**
	 * Remove a room from a table
	 * @param roomID
	 */
	public void removeRoomFromList(String roomID){
		if(this.isRoomPresent(roomID)){
			this.hTable.remove(roomID);
			System.out.println("Room " + roomID + " was remove to the table");
		}
		
	}
	
	/**
	 * Returns a room
	 * @param roomID
	 * @return
	 */
	public RoomInfo getRoom(String roomID){
		return this.hTable.get(roomID);
	}
	
	/**
	 * Set this table with random data
	 * @param list
	 */
	public void setRoomTable(ArrayList<Object> list){
		if(list.size()!=0){
		   for(Object ob:list){
			   
			   String roomID = (String)ob;
			   RoomInfo room = new RoomInfo();
			   room.setRoomID(roomID);
			   String day = dayTab[generator.nextInt(31)];
			   String month = monthTab[generator.nextInt(12)];
			   String year = yearTab[0];
			   String date = day+month+year;
			   room.setDate(date);
			   room.setRoomTaken(generator.nextBoolean());
			   room.setHourStart(getHour(generator)[0]);
			   room.setHourEnd(getHour(generator)[1]);
			   room.setRoomTaken(generator.nextBoolean());
			   this.hTable.put((String) roomID , room);
			   System.out.println("Adding " + room.toString());
			   System.out.println("");
			 }
		}
	}
	
	/**
	 * Set this table with information from a RoomInfoReader
	 * @param list
	 */
	public void setRoomTable2(ArrayList<Object> alist){
		RoomInfoReader rfi = new RoomInfoReader();
		rfi.openList();
		if(this.list.size()!=0 && alist.size() !=0 ){
			
			for(int i = 0; i < alist.size();i++){
				RoomInfo room = this.list.get(i);
				room.setRoomID((String) alist.get(i));
				this.hTable.put((String) alist.get(i), room);
				System.out.println("Adding " + room.toString());
				System.out.println("");
			}
			System.out.println("The table is completed");
		}
	}
	
	/**
	 * Return 
	 * @param generator
	 * @return
	 */
	public String[] getHour(Random generator){
		String [] hourTab = new String[2];
		while(true){
			String start = hourStartTab[generator.nextInt(13)];
			String end = hourEndTab[generator.nextInt(14)];
			if(!start.equals(end) && Integer.parseInt(start) < Integer.parseInt(end)){
				hourTab[0] = start;
				hourTab[1] = end;
				break;
			}
		}
		
		
		return hourTab;
	}
	

	/**
	 * Return the list of RoomInfo with these parameters
	 * @param date
	 * @param hourStart
	 * @param hourEnd
	 * @return
	 */
	public ArrayList<RoomInfo> getListWithCharacteristics(String date, String hourStart,
			String hourEnd){
		ArrayList<RoomInfo> list = new ArrayList<RoomInfo>();
		Enumeration<RoomInfo> emu = this.hTable.elements();
		while(emu.hasMoreElements()){
			RoomInfo room = emu.nextElement();
			if(room.isRoomOneEqualToParameters(date, hourStart, hourEnd)){
				list.add(room);
				//System.out.println("");
				System.out.println("This room match your criterion");
				System.out.println(room.toString());
			}
		}
		
		if(list.isEmpty())
			list =null;
		return list;
		
	}
	/**
	 * Retrieve a room from the list
	 * @param name
	 * @param roomID
	 * @param date
	 * @param hourStart
	 * @param hourEnd
	 * @return
	 */
	public RoomInfo getRoomInfo(String name,String roomID,String date, String hourStart,
			String hourEnd){
		RoomInfo room = null;
		if( name !=null && roomID !=null && date !=null && hourStart !=null && hourEnd !=null ){
			room = this.hTable.get(roomID);
			if(room !=null){
				if(room.isRoomTaken()){
					room.setAvaibilityToTaken(name, date, hourStart, hourEnd);
					this.hTable.put(roomID,room);
				}
			}
			
		}
		return room;
	}
}
